<?php

	$column_expr = array_values($columns);
	$separator = empty($separator) ? ", " : $separator;
	$column_width = empty($column_width) ? array() : $column_width;
	$first_column_action = empty($first_column_action) ? "izmeni/%s" : $first_column_action;

?><table class="entity-table">
<thead>
	<tr>
		<? foreach($columns as $label=>$value_expr): ?>
		<th<?= !empty($column_width[$label]) ? ' width="'.$column_width[$label].'"' : ""; ?>><?= $label ?></th>
		<? endforeach; ?>
		<th></th>
	</tr>
</thead>
<tbody>
	<? if(!empty($rows)): ?>
	
		<? foreach($rows as $k=>$row): ?>
		<?php
			$r_id = Set::extract($row_id, $row);
		?>
		<tr>
			<? foreach ($column_expr as $i=>$expr): ?>
			<td>
				<?php
					if(is_array($expr)) {
						if(isset($expr["value"])) {
						    if(is_array($expr["value"])) {
						        $value = array();
						        foreach($expr["value"] as $k=>$ex) {
						            if($ex) {
						                $v = Set::extract($ex, $row);
						            }else{
						                $v = '';
						            }
						            $value[$k] = $v;
						        }
						    }else{
						        $value = Set::extract($expr["value"], $row);
						    }
						    
							if(isset($expr["function"]) && !empty($value)) {
								$expr["function"] = preg_replace('/;$/', '', $expr['function']) . ';';
								
								if(is_array($value) || is_object($value)) {
									$toeval = '$value = '.str_replace("{VAL}", '$value', $expr['function']);
								}else{
									$toeval = '$value = '.str_replace("{VAL}", $value, $expr['function']);
								}
								eval($toeval);
							}
							$html = $value;
						}else{
							$col = array();
							foreach ($expr as $exp) {
								$v = Set::extract($exp, $row);
								if(!empty($v)) {
									$col[] = $v;
								}
							}
							$html = implode($separator, $col);
						}
					} else {
						$html = Set::extract($expr, $row);
					}
					
					if($i==0) {
						echo $this->Html->link($html, sprintf("$first_column_action", "$r_id"));
					}else{
						echo $html;
					}
				?>
			</td>
			<? endforeach; ?>
			<td class="actions">
				<?= $this->Html->link("Izmeni", "izmeni/".$r_id, array('class'=>'edit', 'title'=>"Izmeni")) ?>
				<?= $this->Html->link("Obriši", "obrisi/".$r_id, array('class'=>'delete', 'title'=>"Obriši", "confirm"=>parse_message($delete_confirm, $row))) ?>
			</td>
		</tr>
		<? endforeach; ?>
	<? else: ?>
	<tr><td colspan="<?= count($column_expr)+1 ?>">Nema rezultata.</td></tr>
	<? endif; ?>
</tbody>
</table>